import {CACHE_SUBSCRIBE_MESSAGE} from './../config.js';

export function auth() {
  let tmplIds = {};
  const messageTmplIds = wx.getStorageSync(CACHE_SUBSCRIBE_MESSAGE);
  tmplIds = messageTmplIds ? JSON.parse(messageTmplIds) : {};
  return tmplIds;
}

/**
 * 附近人
 * 访客来访提醒
 * 推荐成功提醒
 * 积分清零通知
 */
export function openNearbySubscribe(type = 0) {
  const tmplIds = [
    "qBQh2zb-TTOsGKvfpEIvcLa9HdWGOxukzljpaflU7Cs", //来访提醒
    "dn3uetnl7Wip3JbOkMwGL5J6VvcR_RGAnxc6ARl65qY", //推荐成功提醒
    "oDeWlNb_eOYN-512aSNVQvRXcb9SBU2loYmQ952I4Ds"
  ];

  if (type)
    return tmplIds;

  return subscribe(tmplIds);
}


/**
 * 调起订阅界面
 * array tmplIds 模板id
 */
export function subscribe(tmplIds) {
  return new Promise((reslove) => {
    wx.requestSubscribeMessage({
      tmplIds: tmplIds,
      success(res) {
        return reslove(res);
      },
      fail(res) {
        return reslove(res);
      },
      complete: function (res) {
        return reslove(res);
      }
    });
  });
}
